package com.iflytek.uoamp.agent.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.core.agentinterface.IAgent;
/**
 * @ClassName: AgentMain
 * @Description: uoamp-agent启动模块
 * @author qinanyuan
 * @date 2017年3月22日 下午9:08:47
 */
public class AgentMain {

	/**
	 * @Title: main
	 * @Description: uoamp-agent模块启动的主方法
	 * @param args
	 * @throws Exception   void
	 * @date 2017年3月25日 上午10:02:06
	 * @throws
	 */
	public static void main(String[] args) throws Exception {
        //获取实现了顶层接口的模块类
    	ServiceLoader<IAgent> serviceLoader = ServiceLoader.load(IAgent.class);
		Iterator<IAgent> services = serviceLoader.iterator();
		List<IAgent> serviceList = new ArrayList<IAgent>();
		//将加载的类放入serviceList
		while (services.hasNext()) {
			IAgent agentService = services.next();
			serviceList.add(agentService);
		}
   
		//将加载的类按级别进行升序排序
        Collections.sort(serviceList, new ServiceLoaderComparator());
        
        //依次启动相应模块
        for (IAgent agentService : serviceList) {
			//初始化
			agentService.init();
			//启动模块
			agentService.start();
		}
		
	}

}
